﻿namespace Microsoft.ExternalResearch.CreativeCommons.Common
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    /// <summary>
    /// User control that displays the question. Please note that if a question has more than 5 options
    /// a combo box will be used instead of radio buttons to display options.
    /// </summary>
    internal partial class QuestionControl : UserControl
    {
        /// <summary>
        /// Question for the control.
        /// </summary>
        private Question question;

        /// <summary>
        /// Option keys for the options. This is required to store the keys for the options when we display
        /// a combo box instead of radio button.
        /// </summary>
        private string[] optionKeys;

        /// <summary>
        /// Creates an instance of this class.
        /// </summary>
        /// <param name="question">Question object that contains information about the question to be displayed.</param>
        public QuestionControl(Question question)
        {
            this.InitializeComponent();

            this.groupBox1.Text = Helper.GetString("Options");

            this.question = question;

            if (question != null)
            {
                this.labelDescription.Text = question.Description;
                this.labelQuestion.Text = question.Label;

                if (this.UseRadioControl)
                {
                    foreach (string optionKey in question.Options.Keys)
                    {
                        RadioButton b = new RadioButton();
                        b.Text = question.Options[optionKey];
                        b.Tag = optionKey;
                        b.AutoSize = true;
                        b.CheckedChanged += this.CheckedChanged;
                        b.TabStop = true;
                        this.flowLayoutPanelOptions.Controls.Add(b);
                    }

                    // Select first option.
                    ((RadioButton)this.flowLayoutPanelOptions.Controls[0]).Checked = true;
                }
                else
                {
                    ComboBox comboBox = new ComboBox();
                    comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
                    this.optionKeys = new string[this.question.Options.Count];
                    int i = 0;
                    foreach (string optionKey in this.question.Options.Keys)
                    {
                        comboBox.Items.Add(this.question.Options[optionKey]);
                        this.optionKeys[i++] = optionKey;
                    }

                    comboBox.SelectionChangeCommitted += this.CheckedChanged;
                    comboBox.TabStop = true;
                    this.flowLayoutPanelOptions.Controls.Add(comboBox);

                    // Select first option.
                    comboBox.SelectedIndex = 0;
                }
            }
        }


        /// <summary>
        /// Raised when a different option is selected for the question.
        /// </summary>
        public event EventHandler<EventArgs> SelectionChanged;

        /// <summary>
        /// Gets the question object.
        /// </summary>
        public Question Question
        {
            get { return this.question; }
        }

        /// <summary>
        /// Gets the ID of the selected option.
        /// </summary>
        public string Selection
        {
            get
            {
                if (this.UseRadioControl)
                {
                    foreach (RadioButton radioButton in this.flowLayoutPanelOptions.Controls)
                    {
                        if (radioButton.Checked)
                        {
                            return radioButton.Tag as string;
                        }
                    }
                }
                else
                {
                    ComboBox comboBox = (ComboBox)this.flowLayoutPanelOptions.Controls[0];

                    if (!string.IsNullOrEmpty(comboBox.Text))
                    {
                        return this.optionKeys[comboBox.SelectedIndex];
                    }
                }

                return null;
            }
        }

        /// <summary>
        /// Gets a value indicating whether we need to display combo box instead of radio buttons.
        /// </summary>
        private bool UseRadioControl
        {
            get
            {
                // We need to use combo box instead of radio buttons when there 
                // are more than 5 answer options available for the question.
                return (this.question.Options.Count <= 5);
            }
        }

        /// <summary>
        /// Raises the "SelectionChanged" event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void CheckedChanged(object sender, EventArgs e)
        {
            // Send only if this is a callback from the newly checked radio button
            // (ignore 'unchecking' events)
            if (!this.UseRadioControl)
            {
                if (!string.IsNullOrEmpty(((ComboBox)sender).Text))
                {
                    EventHandler<EventArgs> evnt = this.SelectionChanged;
                    if (evnt != null)
                    {
                        evnt(sender, e);
                    }
                }
            }
            else if (((RadioButton)sender).Checked)
            {
                EventHandler<EventArgs> evnt = this.SelectionChanged;
                if (evnt != null)
                {
                    evnt(sender, e);
                }
            }
        }
    }
}